package top.zhdev.community.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import javax.annotation.Resource;

/**
 * @author coolsen
 * @version 1.0.0
 * @ClassName RedisConfig.java
 * @Description Redis Config
 * @createTime 5/9/2020 12:04 PM
 */

@Configuration
public class RedisConfig {

    @Resource
    private RedisConnectionFactory factory;

    @Bean
    public RedisTemplate<String,Object> redisTemplate(){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);

        // 设置key的序列化方式
        redisTemplate.setKeySerializer(RedisSerializer.string());
        // 设置value的序列化方式
        redisTemplate.setValueSerializer(RedisSerializer.json());
        // 设置hash的key序列化方式
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        // 设置hash的value序列化方式
        redisTemplate.setHashKeySerializer(RedisSerializer.json());

        // 初始化完成序列化的方法
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
